	
	//header('Content-Type: text/plain');
	$id = mysqli_real_escape_string ($dbc, $_GET['id']);
	// TEST. DISPLAY DIRECT ADS OR AFFILIATE ADS?
	$ad_type_direct = false;
	//Enter your IDs
	define("Access_Key_ID", "AKIAJPT7EI6YQTVC4KGQ");
	define("Associate_tag", "quantsolut06-20");
	define("SecretKey", "Ikjpt2NmriYmfS12V7SPC12/QeMoEDqYBLRQ1irh");
	
	//Set up the operation in the request
	function ItemSearch($SearchIndex, $Keywords){
		//Set the values for some of the parameters.
		$Operation = "ItemSearch";
		$Version = "2011-08-01";
		$ResponseGroup = "Medium,Images,ItemAttributes";
		$timestamp = gmstrftime("%Y-%m-%dT%H:%M:%S.000Z");
		
		//User interface provides values
		//for $SearchIndex and $Keywords
		
		//Define the request
		$request=
		"http://webservices.amazon.com/onca/xml"
		. "?Service=AWSECommerceService"
		. "&AssociateTag=" . Associate_tag
		. "&AWSAccessKeyId=" . Access_Key_ID
		. "&Operation=" . $Operation
		. "&Version=" . $Version
		. "&SearchIndex=" . $SearchIndex
		. "&Keywords=" . $Keywords
		//. "&Signature=" . Signature
		. "&ResponseGroup=" . $ResponseGroup
		. "&Timestamp=" . $timestamp;
		
		//echo "\n";
		//echo "\n";
		//echo $request;

		
		$host = parse_url($request, PHP_URL_HOST);
		$paramstart = strpos( $request, "?");
		$workurl = substr($request,$paramstart+1);

		$params = explode("&", $workurl);

		//print_r($params);

		sort($params);
		//print_r($params);
		$getParams = implode("&", $params);
		$getParams = rawurlencode($getParams);
		$getParams = str_replace("%3D", "=", $getParams);
		$getParams = str_replace("%26", "&", $getParams);
		
		$signstr = "GET\n" . $host . "\n/onca/xml\n" . $getParams;

		$signstr = base64_encode(hash_hmac('sha256', $signstr, SecretKey, true));
		$signstr = urlencode($signstr);
		$signedurl = 'http://' . $host . '/onca/xml?' . $getParams . "&Signature=" . $signstr;
		
		
		//echo "</br>";
		//echo "</br>";
		$request = $signedurl;
		//Catch the response in the $response object
		//echo $request;
		//echo "</br>";
		
		$response = file_get_contents($request);
		
		//echo $response;
		$parsed_xml = simplexml_load_string($response);
		printSearchResults($parsed_xml, $SearchIndex);
	}
	

	function printSearchResults($parsed_xml, $SearchIndex){
	
	
	print("<table>");
	print("<tr><td>");
	//echo $parsed_xml->Items->TotalResults;
	$randomProduct = rand(0, count($parsed_xml->Items->Item)- 1);
	$i = 0;
	//echo "ran=".$randomProduct;
	//echo "items=".count($parsed_xml->Items->Item);
	foreach($parsed_xml->Items->Item as $current){
		
		if ($i == $randomProduct)
		{
			if (isset($current->SmallImage->URL)) {
					$height = $current->SmallImage->Height;
					$width = $current->SmallImage->Width;
					$forcedHeight = 40.0;
					$ratio = $forcedHeight/$height;
					$width = $ratio * $width;
					$height = $forcedHeight;
					print("<a href=\" ". $current->DetailPageURL . "\" target=\"_blank\"><img src=\"".$current->SmallImage->URL."\" 
					height=\"" . $forcedHeight . "px\" width=\"".$width."px\" BORDER=0	
					onClick=\"_gaq.push(['_trackEvent', 'Videos', 'Play', '12345']);\" /></a>");
				} 
			$title = $current->ItemAttributes->Title;
			if (strlen($title) > 60)
			{
				$title = substr($title,0,60)."...";
			}
			print("</td><td width=210px><font size='1' face=\"verdana\" color=#F0F0F0><b>" ."<a href=\"" . $current->DetailPageURL . "\" target=\"_blank\" onClick=\"_gaq.push(['_trackEvent', 'Ads', 'Click', '12345', 123]); \">"  . $title. "</a></b>");	
			if(isset($current->ItemAttributes->ListPrice)) {
					print("<br>Price: ".$current->ItemAttributes->ListPrice->FormattedPrice);
				} 
			print("</td></tr></table>");
			break;
		}
		$i++;
		
	}
	
	/*
	print $parsed_xml->Items->TotalResults;
	foreach($parsed_xml->Items->Item as $current){
		print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>");
		if (isset($current->ItemAttributes->Title)) {
			print("<br>Title: ".$current->ItemAttributes->Title);
		} 
		if(isset($current->ItemAttributes->Author)) {
			print("<br>Author: ".$current->ItemAttributes->Author);
		} 
		if(isset($current->ItemAttributes->ListPrice)) {
			print("<br>Price: ".$current->ItemAttributes->ListPrice->FormattedPrice);
		} 
		
	
		
	}
	  */
	}
	
	
	if ( $id == null ) {
		// Display Generic Ad
		// Display House Ad
	}
	else
	{
		//echo "Client's ID is: " . $id;
		//echo "";
		$query = "SELECT * FROM `clients` WHERE `id`='$id'";
		$r = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($dbc));
		
		if (@mysqli_num_rows($r) == 1)
		{
			$row = $r->fetch_assoc();

			$age = $row['age'];
			$gender = $row['gender']; // MALE = 0 , FEMALE = 1
			$income = $row['income'];
			$occupation = $row['occupation'];
			$interest1 = $row['interest1'];
			$interest2 = $row['interest2'];
			$interest3 = $row['interest3'];
			
			// echo $age . $gender . $income . $occupation . $interest1 . $interest2 . $interest3;
			
			// Match Interests with advertisement
			
			// Display Direct Ads or Affliate Ads?
			$randomNum = rand(0,1);
			if ( $randomNum == 0 )
			{
				$ad_type_direct = false;
			}
			else
			{
				$ad_type_direct = true;
			}
			
			if ($ad_type_direct)
			{
				// Display OpenX Ads
				echo "<script type='text/javascript'><!--// <![CDATA[\n";
				echo "/* [id1] Counterpath Test Client - Default */\n";
				echo "OA_show(1);";
				echo "// ]]> --></script><noscript><a target='_blank' href='http://quantotech.com/openx-2.8.8/www/delivery/ck.php?n=bc56dcf'><img border='0' alt='' src='http://quantotech.com/openx-2.8.8/www/delivery/avw.php?zoneid=1&amp;n=bc56dcf' /></a></noscript>";

			}
			else
			{
				$list = array( "Switching to Voip", "softphone", "SIP", "Voip phones", "voip networks", "voip phone adapter", "voip headset", "voip usb gateway");
				//print_r($list);
				$num2 = rand(0, count($list)-1);
				//$num = rand(1,10);
				//echo $list[$num2];
				ItemSearch("All", $list[$num2] );
			}
		}
		
	}
	
?>
